package com.lagou.task.servlet;

import com.lagou.task.entity.SysClass;
import com.lagou.task.service.ClassService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ClassDeleteServlet",urlPatterns = "/classDelete")
public class ClassDeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassService classService = new ClassService();
        List<SysClass> classes = classService.showAllService();
        String deleteName = request.getParameter("deleteName");
        String[] deleteNames = deleteName.split(" ");
        int deleteNum = deleteNames.length;
        int delete_i = 0;

        for (SysClass sysClass:classes){
            for (String s:deleteNames){
                if (!deleteNames.equals("") && sysClass.getName().equals(s) && sysClass.getNumer() == 0){
                    delete_i += classService.deleteClassService(sysClass);
                }
            }
        }

        if (delete_i == deleteNum){
            response.sendRedirect("class");
        }else{
            System.out.println("删除失败！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
